
import { ml_hideLoading, ml_showLoading, ml_showToast, ml_Toast } from '../../utils/asyncWX'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isTiptrue: false,
    direction_num: 0,//视频转向度数,
    sneg: true,
    swipt_list: [],
    msgList: [],
    search_data: '',
    search_list: [],
    //优惠卷专用
    juanlist: [],
    imgList: ['https://656e-env-7g9v3jl6ff2c202e-1309274557.tcb.qcloud.la/image/img3.027art.jpg', 'https://656e-env-7g9v3jl6ff2c202e-1309274557.tcb.qcloud.la/image/xi.eol.jpg', 'https://656e-env-7g9v3jl6ff2c202e-1309274557.tcb.qcloud.la/image/eol.jpg']
  },
  // 是否静音
  SetShadow() { this.setData({ sneg: !this.data.sneg }); },

  swiper_ip(e) {
    let type = e.currentTarget.dataset.type;
    if (type == 'voiced') {
      this.setData({ direction_num: 90 });
    }
  },


  //获取时间
  gettime(time) {
    var n = parseInt(time);
    var D = new Date(n);
    var year = D.getFullYear();//四位数年份
    var month = D.getMonth() + 1;//月份(0-11),0为一月份
    month = month < 10 ? ('0' + month) : month;

    var day = D.getDate();//月的某一天(1-31)
    day = day < 10 ? ('0' + day) : day;

    var hours = D.getHours();//小时(0-23)
    hours = hours < 10 ? ('0' + hours) : hours;

    var minutes = D.getMinutes();//分钟(0-59)
    minutes = minutes < 10 ? ('0' + minutes) : minutes;
    var now_date = month + '-' + day;
    var now_time = hours + ':' + minutes;
    return { now_date, now_time }
  },

  search_input(e) {
    this.setData({ search_data: e.detail.value });
  },

  search() {
    let dataed = this.data.search_data;
    if (dataed == '') {
      ml_Toast('订单号不能为空！');
      return;
    }
    if (dataed.indexOf('gold') == -1) {
      ml_Toast('订单号格式错误');
      return;
    } else {
      //1646362297864gold
      wx.cloud.callFunction({
        name: 'getopenid', data: {
          type: 'select_order',
          order_code: dataed
        }
      }).then(res => {
        console.log(res.result.data);
        let datasd = res.result.data[0];
        let dats = datasd.order_state;
        for (let j = 0; j < dats.length; j++) {
          let time_data = this.gettime(dats[j].pay_time);
          datasd.order_state[j]['now_date'] = time_data.now_date;
          datasd.order_state[j]['now_time'] = time_data.now_time;
        }
        this.setData({ search_list: datasd });
      })
    }
  },

  //展示收货码
  receipt_code() {
    wx.navigateTo({
      url: '/packageB/pages/Receipt_code/Receipt_code',
    })
  },


  //卡卷包
  Card_roll() {
    wx.navigateTo({
      url: '/packageB/pages/Card_roll/Card_roll',
    })
  },

  // 代收包裹
  Collection_package() {
    let userinfo_inf = wx.getStorageSync('userinfo_inf');
    if (userinfo_inf == '') {
      wx.showModal({
        title: '提示', showCancel: false, content: '请先领取收货码',
        success(res) {
          if (res.confirm) { wx.navigateTo({ url: '/packageB/pages/Receipt_code/Receipt_code', }) }
        }
      });
    } else if (userinfo_inf.login_sincerity < 20) {
      wx.showModal({ title: '严重提示', showCancel: false, content: '你的信誉低，请联系客服增加信誉。', });
    } else {
      wx.navigateTo({
        url: '/packageB/pages/Collection_package/Collection_package',
      })
    }

  },
  //领取优惠卷
  Collar_roll(e) {
    let Collar_inf = e.currentTarget.dataset.item;
    let login_openid = wx.getStorageSync('openid');
    console.log(e.currentTarget.dataset.item);
    let seting = { 'usetype': 'collar', 'collar_id': Collar_inf._id, 'collar_inf': Collar_inf, 'login_openid': login_openid, 'receive_time': +new Date() };
    ml_showLoading();
    wx.cloud.callFunction({ name: 'collar_roll', data: seting }).then(res => {
      console.log(res);
      if (res.result.code == 200) {
        let text = res.result.text;
        this.getCollar_roll();
        ml_showToast(text);
      } else if (res.result.code == 400) {
        ml_Toast(res.result.text);
      }
    }).catch(res => {
      ml_Toast('请求数据失败');
    })
  },

  //整理时间字符
  return_time(object) {
    for (let i = 0; i < object.length; i++) {
      let time_1 = object[i].over_time;
      let unixTimestamp = new Date(time_1);
      let commonTime = unixTimestamp.toLocaleString();
      object[i]['return_time'] = commonTime;
    }
  },
  getswipt_roll() {
    wx.cloud.database().collection('swipt_active').get().then(res => {
      this.setData({ swipt_list: res.data });
    }).catch(res => {
      ml_Toast('获取活动列表失败！请退出重新尝试！')
    })
  },

  getCollar_roll() {
    //获取优惠卷
    ml_showLoading('加载数据中....');
    wx.cloud.callFunction({
      name: 'collar_roll', data: {
        usetype: 'get_home_coupon'
      }
    }).then(res => {
      ml_hideLoading();
      console.log(res);
      let data = res.result.coupon_list.data;
      this.return_time(data);
      this.setData({ juanlist: data, msgList: res.result.notice_list.data });
    }).catch(res => {
      console.log(res);
      ml_Toast('获取优惠卷失败！请重新尝试！')
    })
  },

  new_data() {
    // 在组件实例刚被创建时执行
    let firstOpen = wx.getStorageSync("loadOpen");
    console.log("是否首次打开本页面==", firstOpen)
    if (firstOpen == undefined || firstOpen == '') { //根据缓存周期决定是否显示新手引导
      this.setData({
        isTiptrue: true,
      })
    } else {
      this.setData({
        isTiptrue: false,
      })
    }
  },

  user_yin() {
    wx.setStorageSync('loadOpen', '');
    this.setData({
      isTiptrue: true
    })
  },
  closeThis() {
    wx.setStorageSync('loadOpen', 'OpenTwo');
    this.setData({
      isTiptrue: false
    })
  },

  //停用小程序  project_setiing
  if_notuser() {
    wx.showLoading({ title: '加载数据中....' })
    wx.cloud.database().collection('project_setiing').get().then(res => {
      console.log(res);
      wx.hideLoading();
      if (res.data[0].if_notuser) {
        wx.redirectTo({ url: '/pages/page_notuser/page_notuser' });
      }else{
        this.getCollar_roll();  //获取优惠卷
        this.getswipt_roll();
        this.new_data();
      }
    }).catch(res => {
      wx.showModal({ title: '提示', showCancel: false, content: '请求程序失败，请检测网络。', });
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.if_notuser();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    this.tabBar();
  },
  tabBar() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})